package com.yoona.system.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yoona.common.enums.CommonEnums;
import com.yoona.common.response.BaseResponse;
import com.yoona.common.utils.HttpServletRequestUtil;
import com.yoona.common.utils.PassTimeUtils;
import com.yoona.system.mapper.DynamicCommentsMapper;
import com.yoona.system.mapper.DynamicMapper;
import com.yoona.system.entity.Dynamic;
import com.yoona.system.entity.DynamicComments;
import com.yoona.system.service.IDynamicService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @Email: m15602498163@163.com
 * @Author: yoonada
 * @Date: 2020/9/21
 * @Time: 8:47 下午
 * @Msg:
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DynamicServiceImpl extends ServiceImpl<DynamicMapper, Dynamic> implements IDynamicService {

    private final DynamicMapper dynamicMapper;
    private final DynamicCommentsMapper dynamicCommentsMapper;

    /**
     * @Author: YoonaDa
     * @Email: m15602498163@163.com
     * @CreateTime: 2020/10/18 4:59 下午
     * @Param: [dynamicContent, pictureUrl, avatarUrl, nickName]
     * @Return: com.yoona.common.response.SystemResponse
     * @Description: 发动态
     */
    @Override
    public BaseResponse<Object> publishDynamic(String dynamicContent, String pictureUrl, String avatarUrl, String nickName) {
        HttpServletRequest request = HttpServletRequestUtil.getHttpServletRequest();
        String uid = (String) request.getAttribute("uid");
        Dynamic dynamic = Dynamic
                .builder()
                .uid(uid)
                .avatarUrl(avatarUrl)
                .pictureUrl(pictureUrl)
                .dynamicContent(dynamicContent)
                .nickName(nickName)
                .createTime(new Date())
                .updateTime(new Date())
                .build();
        int num = this.baseMapper.insert(dynamic);
        if (num == 1){
            return BaseResponse.success(CommonEnums.INSERT_SUCCESS);
        }else {
            return BaseResponse.error(CommonEnums.INSERT_ERROR);
        }
    }

    /**
     * @Author: YoonaDa
     * @Email: m15602498163@163.com
     * @CreateTime: 2020/10/18 4:59 下午
     * @Param: [dynamicId]
     * @Return: java.util.List<com.yoona.domain.dynamic.entity.Dynamic>
     * @Description: 根据动态id获取动态详情
     */
    @Override
    public BaseResponse<List<Dynamic>> findDynamicById(String dynamicId) {
        List<Dynamic> dynamic = dynamicMapper.selectDynamicById(dynamicId);
        for(Dynamic dynamicList:dynamic){
            dynamicList.setPassPublishTime(PassTimeUtils.format(dynamicList.getCreateTime()));
//            遍历评论时间
            for(DynamicComments dynamicCommentsList:dynamicList.getDynamicCommentsList()){
                if(dynamicCommentsList.getCommentId()!=null){
                    dynamicCommentsList.setPassCommentCreateTime(PassTimeUtils.format(dynamicCommentsList.getCommentCreateTime()));
                }
            }
            dynamicList.setDynamicCommentsList(dynamicList.getDynamicCommentsList());
            String arrImage = dynamicList.getPictureUrl();
            setListImage(dynamicList, arrImage);
        }
        return BaseResponse.success(dynamic);
    }

    /**
     * @Author: YoonaDa
     * @Email: m15602498163@163.com
     * @CreateTime: 2020/10/25 11:48 上午
     * @Param: [dynamicId]
     * @Return: com.yoona.common.response.SystemResponse
     * @Description: 删除动态
     */
    @Override
    public BaseResponse<Object> deleteDynamic(String dynamicId) {
        Map<String,Object> dynamicMap=new HashMap<>(1);
        dynamicMap.put("dynamic_id",dynamicId);
        int i = this.baseMapper.deleteByMap(dynamicMap);
        if (i==1){
//            删除动态的对应评论
            Map<String,Object> commentMap = new HashMap<>(1);
            commentMap.put("dynamic_id",dynamicId);
            dynamicCommentsMapper.deleteByMap(commentMap);
            return BaseResponse.success(CommonEnums.DELETE_SUCCESS);
        }else {
            return BaseResponse.error(CommonEnums.DELETE_ERROR);
        }
    }


    /**
     * @Author: YoonaDa
     * @Email: m15602498163@163.com
     * @CreateTime: 2020/10/18 5:00 下午
     * @Param: []
     * @Return: java.util.List<com.yoona.domain.dynamic.entity.Dynamic>
     * @Description: 获取个人动态
     */
    @Override
    public BaseResponse<List<Dynamic>> personalDynamic() {
        HttpServletRequest request = HttpServletRequestUtil.getHttpServletRequest();
        String uid = (String) request.getAttribute("uid");
        QueryWrapper<Dynamic> wrapper = new QueryWrapper<>();
        wrapper.eq("uid", uid).orderByDesc("create_time");
        List<Dynamic> dynamics = this.baseMapper.selectList(wrapper);
        if (dynamics.size()>0){
            for (Dynamic dynamicList:dynamics){
                dynamicList.setPassPublishTime(PassTimeUtils.format(dynamicList.getCreateTime()));
                String arrImage = dynamicList.getPictureUrl();
                setListImage(dynamicList, arrImage);
            }
        }
        return BaseResponse.success(dynamics);
    }

    /**
     * 获取所有动态
     * @return
     */
    @Override
    public BaseResponse<List<Dynamic>> selectAllDynamicList() {
        List<Dynamic> allDynamicList = dynamicMapper.selectAllDynamicList();
        if (allDynamicList.size()>0){
            for (Dynamic dynamicList:allDynamicList){
                String arrImage = dynamicList.getPictureUrl();
                dynamicList.setPassPublishTime(PassTimeUtils.format(dynamicList.getCreateTime()));
                setListImage(dynamicList, arrImage);
            }
        }
        return BaseResponse.success(allDynamicList,allDynamicList.size());
    }

    /**
     * @Author: YoonaDa
     * @Email: m15602498163@163.com
     * @CreateTime: 2020/11/5 9:47 下午
     * @Param: [currentPage, size]
     * @Return: com.yoona.common.response.BaseResponse<java.util.List<com.yoona.system.dynamic.entity.Dynamic>>
     * @Description: 根据页码和条数分页
     */
    @Override
    public BaseResponse<List<Dynamic>> selectAllDynamicListByPage(Integer currentPage, Integer size) {
        List<Dynamic> allDynamicList = dynamicMapper.selectAllDynamicListByPage((currentPage-1)*size,size);
        if (allDynamicList.size()>0){
            for (Dynamic dynamicList:allDynamicList){
                String arrImage = dynamicList.getPictureUrl();
                dynamicList.setPassPublishTime(PassTimeUtils.format(dynamicList.getCreateTime()));
                setListImage(dynamicList, arrImage);
            }
        }
        return BaseResponse.success(allDynamicList,dynamicMapper.selectAllDynamicListCount());
    }



    /**
     * 封装一下
     * @param dynamicList
     * @param arrImage
     */
    private void setListImage(Dynamic dynamicList, String arrImage) {
        if (!StringUtils.isBlank(arrImage)) {
            String[] arrImages = arrImage.split(",");
            List<String> list = new ArrayList<String>();
            for (int i = 0; i < arrImages.length; i++) {
                list.add(arrImages[i]);
            }
            dynamicList.setListImage(list);
        }
    }
}
